//
//  SearchViewController.m
//  音悦咖
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SearchViewController.h"
#import "BaseHeader.h"
#import "D_SearchMusicViewController.h"
#import "AFNetworkHandler.h"
@interface SearchViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *dataArr;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic,retain) UIImageView *bgImg;

@property (nonatomic, retain) UIView *bgView;

@end

@implementation SearchViewController

- (void)dealloc
{
    [_search release];
    [_button1 release];
    [_button2 release];
    [_button3 release];
    [_button4 release];
    [_button5 release];
    [super dealloc];
}

#pragma mark - 通知中心

//- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        [self creatNotificationCenter];
//    }
//    return self;
//}
//
//- (void)creatNotificationCenter
//{
//    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//    [center addObserver:self selector:@selector(changeImg:) name:@"imgcom" object:nil];
//}
//
//- (void)changeImg:(NSNotification *)noti
//{
//    NSLog(@"--------%@",noti.object);
//    NSLog(@"--------%@",noti.userInfo);
//    self.bgImg.image = [UIImage imageNamed:noti.object];
//    
//}
//

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    

    self.titleLabel.text = @"搜索";
    self.bgImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.bgImg.image = [UIImage imageNamed:@"img17.jpg"];
  
    [self.view addSubview:self.bgImg];
    [_bgImg release];
     self.bgImg.alpha = 0.8;
    
    
    self.search = [[UISearchBar alloc]initWithFrame:CGRectMake(30, 100, SCREEN_WIDTH - 60, 30)];
    self.search.layer.borderWidth = 2;
    self.search.delegate = self;
    self.search.layer.borderColor = [UIColor grayColor].CGColor;
    self.search.layer.cornerRadius = 10;
    self.search.layer.masksToBounds = YES;
    self.search.placeholder = @"歌曲/歌手/专辑";
    self.search.keyboardType = UIKeyboardTypeDefault;
    
    
    [self.view addSubview:self.search];
    [_search release];

    
    
    
#pragma mark - 获取数据
    [self getData];
    
    
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 130, SCREEN_WIDTH, SCREEN_HEIGHT - 100)];
    self.view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.bgView];
    
    
    
}

- (void)getData
{
    NSString *str = @"http://so.ard.iyyin.com/sug/billboard?app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=8.0&active=1&net=2&openudid=b55e125b1cbcd2f8d637e5573f9a6f6f241e0c3f&idfa=DD02B768-7FEA-48C3-A977-931093C926A0&utdid=VZ5/NAYMGNwDAGGSKGx50btO&alf=201200&bundle_id=com.ttpod.music";
     
    [AFNetworkHandler GETWithAFNByURL:str completion:^(id result) {
        self.dataArr = [NSMutableArray array];
        self.dataArr = [result objectForKey:@"data"];
        
        
        
        // lable1
        self.button1 = [[UILabel alloc]initWithFrame:CGRectMake(40, 180, 130, 30)];
        [self.button1.layer setCornerRadius:10];
        [self.button1.layer setBorderWidth:2];
        self.button1.text = [self.dataArr[0]objectForKey:@"val"];
        [self.button1 setFont:[UIFont fontWithName:nil size:15]];
        self.button1.textAlignment = NSTextAlignmentCenter;
        [self.button1.layer setBorderColor:[UIColor grayColor].CGColor];
        [self.view addSubview:self.button1];
        self.button1.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(button1Acton)];
        [self.button1 addGestureRecognizer:tap];
        [tap release];
        [_button1 release];
        // lable2
        self.button2 = [[UILabel alloc]initWithFrame:CGRectMake(190, 180, 120, 30)];
        self.button2.layer.contentsScale = 20;
        [self.button2.layer setCornerRadius:10];
        [self.button2.layer setBorderWidth:2];
        self.button2.text = [self.dataArr[1]objectForKey:@"val"];
        [self.button2 setFont:[UIFont fontWithName:nil size:15]];
        self.button2.textAlignment = NSTextAlignmentCenter;
        [self.button2.layer setBorderColor:[UIColor grayColor].CGColor];
        self.button2.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(button2Action)];
        [self.button2 addGestureRecognizer:tap2];
        
        [self.view addSubview:self.button2];
        [_button2 release];
        // lable 3
        self.button3 = [[UILabel alloc]initWithFrame:CGRectMake(180, 240, 100, 30)];
        
        self.button3.layer.contentsScale = 20;
        [self.button3.layer setCornerRadius:10];
        [self.button3.layer setBorderWidth:2];
        self.button3.text = [self.dataArr[2]objectForKey:@"val"];
        [self.button3 setFont:[UIFont fontWithName:nil size:15]];
        self.button3.textAlignment = NSTextAlignmentCenter;
        [self.button3.layer setBorderColor:[UIColor grayColor].CGColor];
        self.button3.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(button3Action)];
        [self.button3 addGestureRecognizer:tap3];
        
        [self.view addSubview:self.button3];
        [_button3 release];
        // lable 4
        self.button4 = [[UILabel alloc]initWithFrame:CGRectMake(60, 240, 110, 30)];
        
        self.button4.layer.contentsScale = 20;
        [self.button4.layer setCornerRadius:10];
        [self.button4.layer setBorderWidth:2];
        self.button4.text = [self.dataArr[3]objectForKey:@"val"];
        [self.button4 setFont:[UIFont fontWithName:nil size:15]];
        self.button4.textAlignment = NSTextAlignmentCenter;
        [self.button4.layer setBorderColor:[UIColor grayColor].CGColor];
        self.button4.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(button4Action)];
        [self.button4 addGestureRecognizer:tap4];
        
        [self.view addSubview:self.button4];
        [_button4 release];
        // lable 5
        self.button5 = [[UILabel alloc]initWithFrame:CGRectMake(90, 300, 130, 30)];
        
        self.button5.layer.contentsScale = 20;
        [self.button5.layer setCornerRadius:10];
        [self.button5.layer setBorderWidth:2];
        self.button5.text = [self.dataArr[4]objectForKey:@"val"];
        [self.button5 setFont:[UIFont fontWithName:nil size:15]];
        self.button5.textAlignment = NSTextAlignmentCenter;
        [self.button5.layer setBorderColor:[UIColor grayColor].CGColor];
        self.button5.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap5 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(button5Action)];
        [self.button5 addGestureRecognizer:tap5];
        
        [self.view addSubview:self.button5];
        [_button5 release];
        
  
        // 创建tableView
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(30 , 100 + 30, SCREEN_WIDTH - 60, 150) style:UITableViewStyleGrouped];
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.alpha = 0.8;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:self.tableView];
        [_tableView release];

        
    }];
    
 
    
}

#pragma mark - 搜索协议方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
     D_SearchMusicViewController *search = [[D_SearchMusicViewController alloc]init];
    search.str = searchBar.text;
    [self.navigationController pushViewController:search animated:YES];
    
}
// 输入文本事实更新
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    
    NSString *url = [NSString stringWithFormat:@"http://so.ard.iyyin.com/sug/sugAll?q=%@&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=8.0&active=1&net=2&openudid=b55e125b1cbcd2f8d637e5573f9a6f6f241e0c3f&idfa=DD02B768-7FEA-48C3-A977-931093C926A0&utdid=VZ5%2FNAYMGNwDAGGSKGx50btO&alf=201200&bundle_id=com.ttpod.music",searchText];
    
//    NSString *str = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    NSLog(@"%@",str);
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        self.song = [NSMutableArray array];
        self.singer = [NSMutableArray array];
        self.video = [NSMutableArray array];
        self.keyword = [NSMutableArray array];
       
        self.song = [dic objectForKey:@"song"];
        self.singer = [dic objectForKey:@"singer"];
        self.video = [dic objectForKey:@"video"];
        self.keyword = [dic objectForKey:@"keyword"];
        
        [self.tableView reloadData];
        
    }];

}

#pragma mark - tableView协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    
    for (NSDictionary *dic in self.song) {
        self.arr = [NSMutableArray array];
        [self.arr addObject:[dic objectForKey:@"name"]];
    }
    
    for (NSDictionary *dic in self.singer) {
        [self.arr addObject:[dic objectForKey:@"alias_name"]];
    }
    for (NSDictionary *dic in self.video) {
        [self.arr addObject:[dic objectForKey:@"name"]];
    }
    for (NSDictionary *dic in self.keyword) {
        [self.arr addObject:[dic objectForKey:@"val"]];
    }
    cell.textLabel.text = self.arr[indexPath.row];
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.song.count + self.singer.count + self.video.count + self.keyword.count == 0) {
        self.tableView.hidden = YES;
    }else{
        self.tableView.hidden = NO;
    }
    return self.song.count + self.singer.count + self.video.count + self.keyword.count - 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    D_SearchMusicViewController *search = [[D_SearchMusicViewController alloc]init];
    search.str =  self.arr[indexPath.row];
    [self.navigationController pushViewController:search animated:YES];
    
}





// lable 的触发方法
- (void)button1Acton
{
    D_SearchMusicViewController *search = [[D_SearchMusicViewController alloc]init];
    search.num = 1;
//    [self.navigationController pushViewController:search animated:YES];
    
    self.search.text = [self.dataArr[0]objectForKey:@"val"];
    NSLog(@"button1");
    
}
- (void)button2Action
{
    D_SearchMusicViewController *search = [[D_SearchMusicViewController alloc]init];
//    [self.navigationController pushViewController:search animated:YES];
    search.num = 2;
     self.search.text = [self.dataArr[1]objectForKey:@"val"];
    NSLog(@"button2");
}
- (void)button3Action
{
    D_SearchMusicViewController *search = [[D_SearchMusicViewController alloc]init];
//    [self.navigationController pushViewController:search animated:YES];
     self.search.text = [self.dataArr[2]objectForKey:@"val"];
    search.num = 3;
    NSLog(@"button3");
}
- (void)button4Action
{
    D_SearchMusicViewController *search = [[D_SearchMusicViewController alloc]init];
//    [self.navigationController pushViewController:search animated:YES];
     self.search.text = [self.dataArr[3]objectForKey:@"val"];
    search.num = 4;
    NSLog(@"button4");
}
- (void)button5Action
{
    D_SearchMusicViewController *search = [[D_SearchMusicViewController alloc]init];
//    [self.navigationController pushViewController:search animated:YES];
     self.search.text = [self.dataArr[4]objectForKey:@"val"];
    search.num = 5;
    NSLog(@"button5");
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
